//---------------------------------------------------------------------------- // File: IX_Window.cpp [v2.3] // Type: A Class containing winAPI. // Author: Ken Anderson // Date: 3/6/06 // OS dependant: Windows // // Notes: A class that contains a set of API functions that allows the programmer // to call a set of indirect calls that won't require them to use windows // headers and windows functions in their program. Hopefully this will // allow for strong portability. // // Header file's required: // 1) IX_Window.h -- Contains everything needed to manage the functions & definitions // specifically designed for the Windows Operating system & in addition // contains the definitions of the child class IX_Window derived from // OS_API_TEMPLATE base class. // 2) Resource.h -- Resource ID & defintions, only supported by Windows OS. // 3) -- Not used // 4) -- Standard C\C++ string manipulation routines. (Sprintf) // A) -- Contained within String.h used for Memory manipulations. (Memcpy) //---------------------------------------------------------------------------- #include "IX_Window.h" #include #include ///////////////////////////////////////////////////////////////////////////// //Internal Class Functions/Dependants. ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class/Dependant: IX_Window // Name: Constructor // Type: Constructor // Permission: Public // Desc: Initializes the IXWindow class // Parameters: // 1) HINSTANCE hinst = The instance of the program being started in windows. // 2) WNDPROC lpfnWndProc = The address of our WindowsProc function. // 3) LPTSTR ClassName = The class name of our window's program. // Default: "Unknown IXWindow Class" if parameter is not defined. // 4) LPTSTR WindowName = The name of the window. // 5) INT nCmdShow = The window's visible properties. // 6) INT iX == The x-axis position of the window. // 7) INT iY == The y-axis position of the window. // 8) INT iWidth = The width of the window. // 9) INT iHeight = The height of the window. // Return value: None ////////////////////////////////////////////////////////////////////////////////////////////////////// IX_Window::IX_Window(HINSTANCE hinst, WNDPROC lpfnWndProc, LPTSTR ClassName, LPTSTR WindowName, INT nCmdShow, INT iX, INT iY, INT iWidth, INT iHeight) { //Our class was not defined set as unknown if(ClassName == NULL) ClassName = _T("Unknown IXWindow Class"); if(WindowName == NULL) WindowName = _T("Unknown IXWindow"); if(iX == NULL) iX = CW_USEDEFAULT; if(iY == NULL) iY = CW_USEDEFAULT; if(iHeight == NULL) iHeight = CW_USEDEFAULT; if(iWidth == NULL) iWidth = CW_USEDEFAULT; //Initialize all our member variables. m_lpfnWndProc = lpfnWndProc; m_hinst = hinst; m_hwnd = NULL; m_hicon = NULL; m_nCmdShow = nCmdShow; m_bActive = TRUE; m_iWinX = iX; m_iWinY = iY; m_iWinHeight = iHeight; m_iWinWidth = iWidth; m_hwndWinDepth = 0; m_dwStyle = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_BORDER; //WS_OVERLAPPEDWINDOW | WS_VISIBLE; //WS_POPUP | WS_CAPTION | WS_SIZEBOX | WS_SYSMENU; m_lpMenuName = NULL; m_lpClassName = ClassName; m_lpWindowName = WindowName; m_bIdle = FALSE; //By default, message based. m_hwndCapture = NULL; m_hdc = NULL; m_bBD = FALSE; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Destructor // Date: 2/25/3--11:43 // Type: Destructor // Permission: Public // Desc: Destroy our class and unregister our class in windows. // Parameters: None // Return value: None ////////////////////////////////////////////////////////////////////////////////////////////////////// IX_Window::~IX_Window() { //Null protection. if(m_lpClassName == NULL) return; if(m_hinst == NULL) return; UnregisterClass(m_lpClassName, m_hinst); } ///////////////////////////////////////////////////////////////////////////// //OS Creation & Destruction Functions. ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Create // Overridden: Yes // Date: 3/1/3-- // Type: Program/Window Management // Permission: Public // Desc: Create our window. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::Create() { //Null protection. if(m_hinst == NULL) return ECERR_NULL; if(m_lpfnWndProc == NULL) return ECERR_NULL; //Register our class in windows. RegClass(m_lpfnWndProc); //Create a window with the following parameters. m_hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, //The Extended Style of the window. m_lpClassName, //The name of the class. m_lpWindowName, //The name of the window. m_dwStyle, //The style of the window. m_iWinX, //The x coordinate of the window. m_iWinY, //The y coordinate of the window. m_iWinWidth, //The width of the window. m_iWinHeight, //The height of the window. NULL, NULL,//m_hmenu, //The handle to the parent & menu. m_hinst, NULL); //The instance of the window program & child info. //Window could not be created, return error. if(m_hwnd == NULL) return TranslateError(GetLastError()); //Show & updated Window's window. ShowWindow( m_hwnd, m_nCmdShow ); UpdateWindow( m_hwnd ); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: TextOut // Overridden: Yes // Date: 7/5/3 // Type: Message Out // Permission: Public // Desc: Prints a message to the users screen. // Parameters: // 1) cstr pMessage == The message sent to the screen. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::PrintMsg(cstr pMessage) { //Locals RECT rect; //Null protection. if(m_hwnd == NULL) return ECERR_NULL; if(m_hdc == NULL) return ECERR_NULL; GetClientRect(m_hwnd, &rect); SetBkMode(m_hdc, TRANSPARENT); SetTextColor(m_hdc, RGB(255, 255, 255)); DrawText(m_hdc, pMessage, -1, &rect, DT_WORDBREAK); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Close // Overridden: Yes // Date: 3/1/3--9:48PM // Type: Program/Window Management // Permission: Public // Desc: Post our DestroyWindow Message as soon as we want to close the window. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::Close() { //Null protection if(m_hwnd == NULL) return ECERR_NA; //Call the windows DestroyWindow so that cleanup can be done correctly. DestroyWindow(m_hwnd); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Destroy // Overridden: Yes // Date: 3/1/3--9:48PM // Type: Program/Window Management // Permission: Public // Desc: Destroy our class and unregister our class in windows. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::Destroy() { //Post a quit message. PostQuitMessage(0); return EC_OK; } ///////////////////////////////////////////////////////////////////////////// //Cursor Controls ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetCursor // Overridden: Yes // Date: 12/9/3 // Type: Cursor controls // Permission: Public // Desc: Set's the current window's cursor. If the value specified in pzCursor is set to null then // the cursor is disabled. // Parameters: // 1) cstr pzCursor == A string containing the name & location of the cursor file. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetCursor(cstr pzCursor) { //Locals HCURSOR hcr = NULL; //Null protection. if(m_hinst == NULL) return ECERR_NULL; //If the file name is null, then the cursor will be disabled. if(pzCursor != NULL) { //If the functions can not find the cursor it'll return a media not found error. if((hcr = (HCURSOR) LoadImage(m_hinst, (LPCTSTR) pzCursor, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE)) == NULL) return OSE_MEDIA_NOT_FOUND; } //Use :: resolution scope to fetch the windows SetCursor API. ::SetCursor(hcr); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetCursorPos // Overridden: Yes // Date: 7/18/05 // Type: Cursor controls // Permission: Public // Desc: Returns the current position of the cursor. // Parameters: // 1) long* px == A pointer to the x-axis value that will be returned. // 2) long* py == A pointer to the y-axis value that will be returned. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::GetCursorPos(long* px, long* py) { if((px == NULL) && (py == NULL)) return ECERR_NULL; //Get the position of the cursor. POINT pt; ::GetCursorPos(&pt); if(px != NULL) *px = pt.x; if(py != NULL) *py = pt.y; //Everything is okay. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SeCursorPos // Overridden: Yes // Date: 7/18/05 // Type: Cursor controls // Permission: Public // Desc: Sets the position of the cursor. // Parameters: // 1) int x == An integer x-axis value that will be assigned. // 2) int y == An integer y-axis value that will be assigned. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetCursorPos(int x, int y) { //Set the cursor position and return an okay status. ::SetCursorPos(x, y); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetCapture // Overridden: Yes // Date: 7/17/05 // Type: Cursor controls // Permission: Public // Desc: Captures the mouse movements for the application. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetCapture() { //Null protection if(m_hwnd == NULL) return ECERR_NA; //Capture, save previous capture, and return okay status. m_hwndCapture = ::SetCapture(m_hwnd); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: ReleaseCapture // Overridden: Yes // Date: 7/17/05 // Type: Cursor controls // Permission: Public // Desc: Releases the mouse or restores the previous capture. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::ReleaseCapture() { //Release the capture and restore the previous one. ::ReleaseCapture(); if(m_hwndCapture != NULL) ::SetCapture(m_hwndCapture); return EC_OK; } ///////////////////////////////////////////////////////////////////////////// //System Querying functions. ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: QueryOS // Overridden: Yes // Date: 10/9/3 // Type: System Querying // Permission: Public // Desc: Quries the opearting system's properties and returns them within the query structre. // Parameters: // 1) pOSRect rc == Rectangle structure that contains the dimensions of a rectangle. // Return value: NONE ////////////////////////////////////////////////////////////////////////////////////////////////////// void IX_Window::QueryOS(POSQuery qc) { //Local structures required to collect data. OSVERSIONINFO ovi; //Initialization of structures that require it. ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); //Begin querying qc->bGUI = 1; //All windows are GUI SystemParametersInfo(SPI_GETWORKAREA, NULL, &(qc->rcWORKEAREA), NULL); //Fetch work area GetVersionEx(&ovi); //Get Version Info. switch(ovi.dwPlatformId) { //Win 3.1...why the hell are we running this on windows 3.1? case VER_PLATFORM_WIN32s: qc->zcPLATFORM = new unchar[strlen("Windows 3.1\0")]; qc->zcPLATFORM = "Windows 3.1\0"; break; //Win ME, 98, or 95. case VER_PLATFORM_WIN32_WINDOWS: if(ovi.dwMinorVersion >= 5) { qc->zcPLATFORM = new unchar[strlen("Windows ME\0")]; qc->zcPLATFORM = "Windows ME\0"; } else if(ovi.dwMajorVersion == 4) { qc->zcPLATFORM = new unchar[strlen("Windows 98\0")]; qc->zcPLATFORM = "Windows 98\0"; } else { qc->zcPLATFORM = new unchar[strlen("Windows 95\0")]; qc->zcPLATFORM = "Windows 95\0"; } break; //WinNT, 2000, & XP case VER_PLATFORM_WIN32_NT: if(ovi.dwMajorVersion >=5) if(ovi.dwMinorVersion = 0) { qc->zcPLATFORM = new unchar[strlen("Windows 2000\0")]; qc->zcPLATFORM = "Windows 2000\0"; } else { qc->zcPLATFORM = new unchar[strlen("Windows XP\0")]; qc->zcPLATFORM = "Windows XP\0"; } else { qc->zcPLATFORM = new unchar[strlen("Windows NT\0")]; qc->zcPLATFORM = "Windows NT\0"; } break; } //Odd, a windows version above wasn't detected? if(qc->zcPLATFORM == NULL) { qc->zcPLATFORM = new unchar[strlen("Windows Unknown\0")]; qc->zcPLATFORM = "Windows Unknown\0"; } return; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetOS // Overridden: Yes // Date: 12/1/3 // Type: System Querying // Permission: Public // Desc: Returns the name/tag of the Operating System in the form of a null-terminating string. // This was implemented for a quick way to query the OS itself rather than getting information // about the platform, version and so forth. // Parameters: // Return value: // cstr == Returns a string that contains the Operating Systems name. // Returns MEMORYERROR if there isn't enough memory. ////////////////////////////////////////////////////////////////////////////////////////////////////// cstr IX_Window::GetOS() { //Locals OSQuery OSq; cstr pcDest; int iLoc; //Query the operating system. QueryOS(&OSq); //Find the first string in the platform name. //Platform names ususally contain the OS and its type seperated by a space. iLoc = strcspn(OSq.zcPLATFORM, " "); //Get the location of the first space. //If no space, return the full platform name. if(iLoc == 0) return OSq.zcPLATFORM; //Allocate memory for the destination. if( (pcDest = new char[iLoc]) == NULL) return "MEMORYERROR\0"; //Copy the operating systems tag only. strncpy(pcDest, OSq.zcPLATFORM, (sizeof(char) * iLoc)); //Null terminate the string pcDest[iLoc] = '\0'; //Return the string. return pcDest; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetClientSize // Overridden: Yes // Date: 3/4/3 // Type: System Querying // Permission: Public // Desc: Return the size of the client area. This does not consider the menu or title bars. // Parameters: // 1) pOSRect rc == Rectangle structure that contains the dimensions of a rectangle. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::GetClientSize(POSRect rc) { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Locals RECT RC; GetClientRect(m_hwnd, &RC); rc->x = RC.left; rc->y = RC.top; rc->x2 = RC.right; rc->y2 = RC.bottom; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetClientSize // Overridden: Yes // Date: 3/4/3 // Type: System Querying // Permission: Public // Desc: This function fetchs the size of the ENTIRE window and returns the information in a OSRect // structure. // Parameters: // 1) pOSRect rc == Rectangle structure that contains the dimensions of a rectangle. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::GetWindowSize(POSRect rc) { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Locals RECT RC; GetWindowRect(m_hwnd, &RC); rc->x = RC.left; rc->y = RC.top; rc->x2 = RC.right; rc->y2 = RC.bottom; return EC_OK; } ///////////////////////////////////////////////////////////////////////////// //Window Manipulations ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowPos // Overridden: Yes // Date: 12/9/3--12:53PM // Type: Window Manipulation. // Permission: Public // Desc: Changes the location(x & y), the size(Width & Height) or the order of the window. // Parameters: // 1) int ix == Desired location on the x-axis to move the window. // 2) int iy == Desired location on the y-axis to move the window. // 3) int iWidth == Desired Width to resize the window to. // 4) int iHeight == Desired Height to resize the window to. // 5) ZOrder zOrder == Desired order of the window. // ZO_BOTTOM == Places the window behind all windows on the bottom of the order. // ZO_NOTOPMOST == Places the window behind all topmost windows. // ZO_TOP == Places the window on top. // ZO_TOPMOST == Places the window on top and makes it gives it 'always on top' status. // 6) uint uFlag == Reserved for future use. Currently always shows the window. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowPos(int ix, int iy, int iWidth, int iHeight, OSZOrder zOrder, uint uFlag) { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Locals HWND hwndOrder; switch(zOrder) { case ZO_BOTTOM: hwndOrder = HWND_BOTTOM; break; case ZO_NOTOPMOST: hwndOrder = HWND_NOTOPMOST; break; case ZO_TOP: hwndOrder = HWND_TOP; break; case ZO_TOPMOST: hwndOrder = HWND_TOPMOST; break; } //Set the window's depth and other quick //access variables. m_hwndWinDepth = hwndOrder; m_iWinX = ix; m_iWinY = iy; m_iWinHeight = iHeight; m_iWinWidth = iWidth; //Use the :: scope resolution to access the windows SetWindowPos. ::SetWindowPos(m_hwnd, hwndOrder, ix, iy, iWidth, iHeight, SWP_SHOWWINDOW); //Everything is okay. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowPos [v2.2] // Overridden: Yes // Date: 3/6/06--1am // Type: Window Manipulation. // Permission: Public // Desc: Changes the size of the window only. // Parameters: // 1) int iWidth == Desired Width to resize the window to. // 2) int iHeight == Desired Height to resize the window to. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowSize(int iWidth, int iHeight) { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Set the window's depth and other quick //access variables. m_iWinHeight = iHeight; m_iWinWidth = iWidth; //Use the :: scope resolution to access the windows SetWindowPos. ::SetWindowPos(m_hwnd, m_hwndWinDepth, m_iWinX, m_iWinY, iWidth, iHeight, SWP_SHOWWINDOW); //Everything is okay. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowSytle // Overridden: Yes // Date: 12/9/3--12:53PM // Type: Window Manipulation. // Permission: Public // Desc: Changes the style of the window. // Parameters: // 1) uint Style == An unsigned integer that contains the desired style of the window. // SEE OS_Types.h documentation for more information on the styles available. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowStyle(uint uStyle) { UINT style = 0; if(uStyle & OS_WS_BORDER) style |= WS_BORDER; if(uStyle & OS_WS_CAPTION) style |= WS_CAPTION; if(uStyle & OS_WS_CHILD) style |= WS_CHILD; if(uStyle & OS_WS_CLIPCHILDREN) style |= WS_CLIPCHILDREN; if(uStyle & OS_WS_CLIPSIBLINGS) style |= WS_CLIPSIBLINGS; if(uStyle & OS_WS_DISABLED) style |= WS_DISABLED; if(uStyle & OS_WS_DLGFRAME) style |= WS_DLGFRAME; if(uStyle & OS_WS_GROUP) style |= WS_GROUP; if(uStyle & OS_WS_HSCROLL) style |= WS_HSCROLL; if(uStyle & OS_WS_VSCROLL) style |= WS_VSCROLL; if(uStyle & OS_WS_MAXIMIZE) style |= WS_MAXIMIZE; if(uStyle & OS_WS_MAXIMIZEBOX) style |= WS_MAXIMIZEBOX; if(uStyle & OS_WS_MINIMIZE) style |= WS_MINIMIZE; if(uStyle & OS_WS_MINIMIZEBOX) style |= WS_MINIMIZEBOX; if(uStyle & OS_WS_OVERLAPPED) style |= WS_OVERLAPPED; if(uStyle & OS_WS_OVERLAPPEDWINDOW) style |= WS_OVERLAPPEDWINDOW; if(uStyle & OS_WS_POPUP) style |= WS_POPUP; if(uStyle & OS_WS_POPUPWINDOW) style |= WS_POPUPWINDOW; if(uStyle & OS_WS_SYSMENU) style |= WS_SYSMENU; if(uStyle & OS_WS_TABSTOP) style |= WS_TABSTOP; if(uStyle & OS_WS_THICKFRAME) style |= WS_THICKFRAME; if(uStyle & OS_WS_VISIBLE) style |= WS_VISIBLE; //Change the window's style. SetWindowLong(m_hwnd, GWL_STYLE, style); //Everything is okay. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowTitle // Overridden: Yes // Date: 9/2/05 // Type: Window Manipulation. // Permission: Public // Desc: Set's the window's title. // Parameters: // 1) cstr strTitle == The title of the window. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowTitle(cstr strTitle) { SetWindowText(m_hwnd, (LPCTSTR)strTitle); return EC_OK; //Everything is okay. } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Timer // Overridden: Yes // Date: 11/16/04 // Type: Application Tool Management // Permission: Public // Desc: Provides timer functionality, including high performance timer functionality if it is // available, which is based on the system's hardware. // Parameters: // 1) OSTimerState TS == The Timer State which activate, stop, and return time based on value. // TS_START == Starts the timer, even if it has been paused, picks up from where it was last stopped. // TS_PAUSE == Pauses the timer. // TS_STOP == Stops and restarts the timer. // TS_ADVANCE == Advances the timer by 1/10th of a second. // TS_GETCOMPLETETIME == Returns the complete time. // TS_GETSESSIONTIME == Returns the total time of the session which is the time between the // start of the timer to when it was paused. // TS_GETELAPSEDTIME == Returns the time from the least time the elapsed time was taken. // Return value: // float == Returns the actual value in seconds of the time that has elapsed. // 0.0f == Will be returned if a command is used that modifies the timer. // -1.0f == Will be returned if the function is not supported. // -2.0f == Will be returned if a command supplied is not provided by the timer. ////////////////////////////////////////////////////////////////////////////////////////////////////// float IX_Window::Timer(OSTimerState TS) { //Locals static BOOL bHPC = false; //High Performance Counter Available static bool bInit = false; //Check for initialization which maintains collects the freq before starting. //Windows specific types static LONGLONG llFreq = 0; //When the time was started (Longlong as double type) LARGE_INTEGER liFreq; //A large integer if(!bInit) { //Fetch the Frequency of the Performance Counter //If it fails, it will have returned false, while if it succeeds //then it will hvae return true & the frequency value. if( (bHPC = QueryPerformanceFrequency(&liFreq)) == TRUE) llFreq = liFreq.QuadPart; //Get the larget 64-bit value. bInit = true; //Initialization over. } //High Performance Counter supported. if(bHPC) { //High Performance Variables. static LONGLONG llStartTime = 0; static LONGLONG llStopTime = 0; static LONGLONG llElapsed = 0; float fElapsedTime = 0.0f; double dSessTime = 0.0; LARGE_INTEGER liCounter; //If the timer has paused, then no new counter values are collected. //Collect a new count value only if the timer has started, the user //wishes to view complete time or if( (TS != TS_START) && (TS != TS_GETCOMPLETETIME) && (llStopTime != 0)) liCounter.QuadPart = llStopTime; else QueryPerformanceCounter(&liCounter); //Examine the different command states switch(TS) { case TS_START: //Timer has started. llStartTime += liCounter.QuadPart - llStopTime; //Reset the start time by ignoring the time stopped. llElapsed = liCounter.QuadPart; //Always count elapsed time. llStopTime = 0; //Reset Stop time to zero. return 0.0f; case TS_STOP: //Timer has stopped and has been reset. llStopTime = liCounter.QuadPart; llStartTime = liCounter.QuadPart; //Reset the start count. llElapsed = liCounter.QuadPart; //Reset the elapsed time. return 0.0f; case TS_PAUSE: //Pauses the timer. llStopTime = liCounter.QuadPart; //Sets the stop time to the current time. llElapsed = liCounter.QuadPart; //Set the elapsed time to when the timer was paused. return 0.0f; case TS_ADVANCE: //Advance the time by 1/10th of a second. llStopTime += llFreq / 10; //Advanced the frequency return 0.0f; case TS_GETCOMPLETETIME: //Fetch the complete time since windows started. return (float) (liCounter.QuadPart / (double)llFreq); //The complete count divided by the frequency. case TS_GETSESSIONTIME: dSessTime = (double) ((liCounter.QuadPart - llStartTime) / (double) llFreq); return (float) dSessTime; //return (float) ((liCounter.QuadPart - llStartTime) / (double) llFreq); //The session time subtracting the start time & divide by the frequency. case TS_GETELAPSEDTIME: //Return the time elapsed since the last elapsed call. fElapsedTime = (float) ((liCounter.QuadPart - llElapsed) / (double) llFreq); llElapsed = liCounter.QuadPart; return fElapsedTime; } } else //High Performance Counter not supported. { //High Performance Variables. static double dStartTime = 0; static double dStopTime = 0; static double dElapsed = 0; float fElapsedTime = 0.0f; float fTime = 0.0f; //If the timer has paused, then no new counter values are collected. //Collect a new count value only if the timer has started, the user //wishes to view complete time or if( (TS != TS_START) && (TS != TS_GETCOMPLETETIME) && (dStopTime != 0)) fTime = (float)dStopTime; else fTime = (float)timeGetTime() * 0.001f; //Examine the different command states switch(TS) { case TS_START: //Timer has started. dStartTime += (double)(fTime - dStopTime); //Reset the start time by ignoring the time stopped. dElapsed = fTime; //Always count elapsed time. dStopTime = 0; //Reset Stop time to zero. return 0.0f; case TS_STOP: //Timer has stopped and has been reset. dStartTime = fTime; //Reset the start count. dElapsed = fTime; //Reset the elapsed time. return 0.0f; case TS_PAUSE: //Pauses the timer. dStopTime = fTime; //Sets the stop time to the current time. dElapsed = fTime; //Set the elapsed time to when the timer was paused. return 0.0f; case TS_ADVANCE: //Advance the time by 1/10th of a second. dStopTime += 1.0f; //Advanced the frequency return 0.0f; case TS_GETCOMPLETETIME: //Fetch the complete time since windows started. return fTime; case TS_GETSESSIONTIME: return (float)(fTime - dStartTime); case TS_GETELAPSEDTIME: //Return the time elapsed since the last elapsed call. fElapsedTime = (float)(fTime - dElapsed); dElapsed = fTime; return fElapsedTime; } } return -2.0f; } ///////////////////////////////////////////////////////////////////////////// //OS Message Handling System ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: Quit // Overridden: Yes // Date: 3/2/3--7:39PM // Type: Message Handling // Permission: Public // Desc: Check the current operating message state and if the state is OSMSG_QUIT, // the return true, else return false. // Parameters: // 1) OS_MSG state == Operating Message State. // Return value: // Bool == Returns true if the program is shutting down. ////////////////////////////////////////////////////////////////////////////////////////////////////// bool IX_Window::Quit(MSG_EVENT state) { if(state == OSM_QUIT) return true; return false; } ///////////////////////////////////////////////////////////////////////////// //Paint & Drawing Functions ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: BeginDraw // Overridden: Yes // Date: 11/19/3 // Type: Painting & Drawing // Permission: Public // Desc: Calls the operating systems begin paint operations. // Parameters: NA // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::BeginDraw() { //Begin drawing has already been called, call EndDraw to close it. if(m_bBD == TRUE) EndPaint(m_hwnd, &m_ps); //Begin painting after retrieving the windows dc. m_hdc = BeginPaint(m_hwnd, &m_ps); //Mark the fact that BeginDraw has been called. m_bBD = TRUE; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: EndDraw // Overridden: Yes // Date: 11/19/3 // Type: Painting & Drawing // Permission: Public // Desc: Calls the operating systems end paint operations. // Parameters: NA // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::EndDraw() { //Being draw was never called, ignore. if(m_bBD == FALSE) return EC_OK; //Begin painting after retrieving the windows dc. EndPaint(m_hwnd, &m_ps); //Mark the fact that EndDraw has been called, closing the painting routines. m_bBD = FALSE; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: MapBitmapToScreen // Overridden: No -- OS dependant // Date: 3/4/3 // Type: Painting & Drawing // Permission: Private // Desc: Maps a bitmap from the CiMedia object and draws it to the screen. // Parameters: // 1) CiMedia* pciMedia == A pointer to a CiMedia object that contains the bitmap to be drawn to the screen. // 2) INT iX == The location on the x axis to display the bitmap. // Default: Sets the image at the 0 pixel on the x axis. // 3) INT iY == The location on the y axis to display the bitmap. // Default: Sets the image at the 0 pixel on the y axis. // 4) INT iWidth == The width of the bitmap. // Default: Uses the bitmap's width. // 5) INT iHeight == The height of the bitmap. // Default: Uses the bitmap's height. // 6) HDC hdc == The main handle to the device context we nee in order to paint to the screen. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::MapBitmapToScreen(HDC hdc, PBitmap pbm, INT iX, INT iY, INT iWidth, INT iHeight) { //If the PBitmap goes null do not change the g_hbmpDisplay if(pbm == NULL) return OSE_NULL_IMAGE; //Local variables for windows conversion. BITMAP bmp; LPVOID lpvBits; HDC hDCImage; HBITMAP hstream; //Translation bitmap structure from ours to windows. TWINBMPS twbs; //Copy PBitmap header & palette to the windows BITMAP structure. memcpy(&twbs.bi, &pbm->bi, sizeof(BitmapImageInfo)); if(twbs.bi.biXPelsPerMeter == 0) twbs.bi.biXPelsPerMeter = 2979; if(twbs.bi.biYPelsPerMeter == 0) twbs.bi.biYPelsPerMeter = 2979; if(pbm->palette != NULL) memcpy(&twbs.bmiColors, pbm->palette, sizeof(ColorARGB)*256 ); //Create a compatible DC to manipulate our image using the current dc. if( (hDCImage = CreateCompatibleDC(hdc)) == NULL) return ECERR_OUTOFMEM; //Create a DIB section to load our bitmap into. hstream = CreateDIBSection( hDCImage, (LPBITMAPINFO)&twbs, DIB_RGB_COLORS, (VOID**)&lpvBits, NULL, NULL); //Problems, clean up & exit. if(lpvBits == NULL || hstream == NULL) { DeleteDC( hDCImage ); return ECERR_OUTOFMEM; } //Copy the image's bits into the DIB section. memcpy(lpvBits, pbm->vpBits, twbs.bi.biSizeImage); //Get the windows compact BITMAP structure. GetObject(hstream, sizeof(BITMAP), &bmp); //Save our previous bitmap, and load our current bitmap. HBITMAP hold = (HBITMAP) SelectObject( hDCImage, hstream ); SetStretchBltMode(hdc, COLORONCOLOR); //Transfer the image bit by bit to the screen. StretchBlt( hdc, iX, iY, iWidth, iHeight, hDCImage, 0, 0, pbm->bi.Width, pbm->bi.Height, SRCCOPY); GetObject(hstream, sizeof(BITMAP), &bmp); //Restore previous dc; SelectObject(hDCImage, hold); //Clean up dc & our DIB being streamed DeleteDC( hDCImage ); DeleteObject( (HBITMAP) hstream); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: DrawMedia // Overridden: Yes // Date: 3/4/3 // Type: Painting & Drawing // Permission: Public // Desc: Writes the image that will be displayed to the screen, all translations such as resizing or // manipulating the image in any way must be done before this call. // IE: This system uses a bitmap and calls this function. // Parameters: // 1) CiMedia* pciMedia == A pointer to a CiMedia object which contains the image to draw to screen. // 2) INT iX == The location on the x axis to display the bitmap. // Default: Sets the image at the 0 pixel on the x axis. // 3) INT iY == The location on the y axis to display the bitmap. // Default: Sets the image at the 0 pixel on the y axis. // 4) INT iWidth == The width of the bitmap. // Default: Uses the bitmap's width. // 5) INT iHeight == The height of the bitmap. // Default: Uses the bitmap's height. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::DrawMedia(CiMedia* pciMedia, INT iX, INT iY, INT iWidth, INT iHeight) { if(pciMedia == NULL) return OSE_MEDIA_NA; //If the main handle to the DC(Device Context) is null, then return an error, do not attempt //to draw with a null handle. if(m_hdc == NULL || m_bBD == FALSE) { PAINTSTRUCT ps; RECT rc; GetClientRect(m_hwnd, &rc); HDC hdc = BeginPaint(m_hwnd, &ps); Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom); DrawText(hdc, "BeginDraw has not been called.", -1, &rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER); EndPaint(m_hwnd, &ps); return OSE_INVALID_HANDLE; } switch(pciMedia->GetMediaType()) { case MT_IMBITMAP: { PBitmap pbm = (PBitmap)pciMedia->GetMedia(); return MapBitmapToScreen(m_hdc, pbm, (INT)iX, (INT)iY, (INT)iWidth, (INT)iHeight); } default: return OSE_MEDIA_NA; } } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: DrawMedia // Overridden: Yes // Date: 3/4/3 // Type: Painting & Drawing // Permission: Public // Desc: Writes the image that will be displayed to the screen. Since no parameters // are provided for the properties of the screen, the media will be mapped // exactly to the size of the window. // IE: This system uses a bitmap and calls this function. // Parameters: // 1) CiMedia* pciMedia == A pointer to a CiMedia object which contains the image to draw to screen. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::DrawMedia(CiMedia* pvMedia) { //Locals RECT rc; GetClientRect(m_hwnd, &rc); DrawMedia(pvMedia, 0, 0, (rc.right - rc.left), (rc.bottom-rc.top)); //Everything is okay. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: DrawRectangle // Overridden: Yes // Date: 12/3/3 // Type: Painting & Drawing // Permission: Public // Desc: Draws a rectangle. Color is broken into intensities due to windows GUI works with BBGGRR // rather than RRGGBB. // Parameters: // 1) OSRect rc == Draws a rectangle. // 2) Byte Red == Red intensity. // 3) Byte Blue == Blue intensity. // 4) BYte Green == Green intensity. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::DrawRectangle(OSRect rc, Byte Red, Byte Blue, Byte Green) { HBRUSH hbrold, hbrnew; //Set the color of the rectangle. hbrnew = CreateSolidBrush(RGB(Red, Blue, Green)); hbrold = (HBRUSH) SelectObject(m_hdc, (HBRUSH)hbrnew); //Draw Rectangle. Rectangle(m_hdc, rc.x, rc.y, rc.x2, rc.y2); //Restore old settings & clean up. SelectObject(m_hdc, (HBRUSH)hbrold); DeleteObject((HBRUSH)hbrnew); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: MsgBox // Overridden: Yes // Date: 2/26/06 // Type: Painting & Drawing // Permission: Public // Desc: Displays a message box with the message supplied. // Parameters: // 1) cstr sMsg == A common string that contains the message to display. // 2) cstr sCaption == A common string that contains the caption to display. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::MsgBox(cstr sMsg, cstr sCap) { //Protections if((sMsg == NULL) || (sCap == NULL) || (m_hwnd == NULL)) return ECERR_NULL; //Display the message ::MessageBox(m_hwnd, sMsg, sCap, MB_OK); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: InvalidateDisplay // Overridden: Yes // Date: 3/30/2005 // Type: Painting & Drawing // Permission: Public // Desc: Invalidates the entire window's region so that the OS will erase and update the display. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::InvalidateDisplay() { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Update the entire window. InvalidateRect(m_hwnd, NULL, TRUE); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: UpdateDisplay // Overridden: Yes // Date: 3/6/2006 // Type: Painting & Drawing // Permission: Public // Desc: Generates a paint message for the operating system to update the display. // Parameters: None // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::UpdateDisplay() { //Null protection. if(m_hwnd == NULL) return ECERR_NULL; //Update the entire window. UpdateWindow(m_hwnd); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: CreateFontImageMap // Overridden: Yes // Date: 3/23/05 // Type: Painting & Drawing // Permission: Public // Desc: Generates an image/texture of a font set, returning the image in a CiMedia objec type, // as well as returning a set of coordinates. // Note: Source information on how to generate a image based on a font map was provided // by Microsoft. // Parameters: // 1) PFONTMAP pfm == A pointer to a font map object that is to be created. // If the process fails, an error code will be returned and the font // map will be initialized to null. // NOTE: Caution should be taken when using a font map as it deals with a set of pointers // that need to be cleaned up by the user. // 2) cstr sFntName == A string describing the name of the font name. // By Default: Arial. // 3) int nFntHeight == The height/size of the font. // By Default: 12 point. // 4) float fScale == The scale factor of the font. // By Default: 1.0f. // 5) int nImgHeight == The height of the image. // By Default: 256 pixels. // 6) int nImgWidth == The width of the image. // By Default: 256 pixels. // 7) bool bBold == Enables or disables the bold attribute. // By default: false. // 8) bool bItalic == Enables or disables the italic attribute. // By default: false. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::CreateFontImageMap(PFONTMAP pfm, cstr sFntName, int nFntHeight, float fScale, Dword dwImgHeight, Dword dwImgWidth, bool bBold, bool bItalic) { //A null font map will cause the function //to exit. if(pfm == NULL) return ECERR_NULL; //Constants const INT nChars = 96; //Full array of printable characters. //----Locals----// //Win specific HDC hDC; //Handle to the new Device Context. HFONT hFont; //Handle to a font. HBITMAP hbmBitmap; //Handle to a bitmap. BITMAPINFO bmi; //Bitmap header info SIZE size; //Size of each character. //Image Variables. INT nWidthBytes=0; //Padded bytes used for the windows scanline properties. CiMedia* pciMedia = NULL; //Media object. BYTE* pBitmapBits = NULL; //Pointer to a block of bitmap bytes. BYTE* pImgBits = NULL; //Pointer to an allocated block of memory from the CiMedia obj. //Font & mapping variables INT nHeight; //Logical coordinates of the font. float fMaxHeight = 0.0f; //Maximum height detected. float fMaxWidth = 0.0f; //Maximum width detected. DWORD x=0, y=0; //Character Coordinates PFRect pfrc=NULL; //An array of coordinates of the character's size. //////////////////////////////////////////////// // Windows Resource Allocation & Preperation // //////////////////////////////////////////////// //Create the bitmap header and initialize with zeros. ZeroMemory( &bmi.bmiHeader, sizeof(BITMAPINFOHEADER) ); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = dwImgWidth; bmi.bmiHeader.biHeight = -(signed)dwImgHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biCompression = BI_RGB; bmi.bmiHeader.biBitCount = 24; //Create the windows resources, handle to DC & Bitmap hDC = CreateCompatibleDC(NULL); hbmBitmap = CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (VOID**)&pBitmapBits, NULL, 0); //Out of memory issues. if(hbmBitmap == NULL) { //Clean up & release all resources no longer needed. DeleteDC( hDC ); return ECERR_INVALID; } //Set the mapping mode for the text coordinates. SetMapMode( hDC, MM_TEXT ); //Calculate the height of the font nHeight = -MulDiv(nFntHeight, (INT)(GetDeviceCaps(hDC, LOGPIXELSY) * fScale), 72 ); //Create the font, request antialiasing. hFont = CreateFont(nHeight, 0, 0, 0, (bBold*FW_BOLD), bItalic, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, VARIABLE_PITCH, sFntName); //Invalid parameters. if( NULL==hFont ) { //Clean up & release all resources no longer needed. DeleteObject( hbmBitmap ); DeleteDC( hDC ); return ECERR_INVALID; } //Select the bitmap and font to use into the DC. SelectObject( hDC, hbmBitmap ); SelectObject( hDC, hFont ); // Set text properties SetTextColor( hDC, RGB(255,255,255) ); //White text. SetBkColor( hDC, 0x00000000 ); //Black background. SetTextAlign( hDC, TA_TOP ); //Align the text to the top. //////////////////////////////////////////////// // Coordinate allocation and protection // //////////////////////////////////////////////// //Allocate memory for the entire printable //character set. if((pfrc = new FRect[nChars]) == NULL) { //Clean up & release all resources no longer needed. DeleteObject(hFont); DeleteObject(hbmBitmap); DeleteDC(hDC); return ECERR_OUTOFMEM; } else //Memory allocation successful pfm->pnCoordNum = nChars; //Update the map to reflect the number of printable characters. //Allocate memory for the media object. //If the memory allocation fails, clean up all //windows objects, and the coordinates of the //texture coordinates. if((pciMedia = new CiMedia) == NULL) { //Clean up & release all resources no longer needed. DeleteObject(hFont); DeleteObject(hbmBitmap); DeleteDC(hDC); //Clean up coordinates. SDELETE(pfrc); return ECERR_OUTOFMEM; } //////////////////////////////////////////////// // Character Mapping to Bitmap Translation // //////////////////////////////////////////////// unchar str[2] = "x"; //Zero initialize the string //Cycle through all printable characters for now. //Note: One less character as the last is a Max coordinates. for(unchar c=0; c<(nChars-1); c++ ) { //Shift to the next character str[0] = c+32; str[1] = '\0'; //Fetch the size of the text. GetTextExtentPoint32( hDC, str, 1, &size ); //If the size of the next character would exceed //the size of the image, drop the character //down to the next line. if((x+size.cx+1) > (unsigned int)dwImgWidth) { x = 0; //Reset the x-axis point y += size.cy+1; //Inc the y-axis point. } //Print the text to the image. ExtTextOut( hDC, x+0, y+0, ETO_OPAQUE, NULL, str, 1, NULL ); //Only calculate the coordinates if the memory could be //allocated, and if the user specified recieving them. if(pfrc != NULL) { //Generate the coordinates of each character in the image map pfrc[c].x = ((float)x)/dwImgWidth; pfrc[c].y = ((float)y)/dwImgHeight; pfrc[c].x2 = ((float)(x+size.cx)/dwImgWidth); pfrc[c].y2 = ((float)(y+size.cy)/dwImgHeight); //Record the maximum width & height. if(fMaxWidth < (pfrc[c].x2 - pfrc[c].x)) fMaxWidth = (pfrc[c].x2 - pfrc[c].x); if(fMaxHeight < (pfrc[c].y2 - pfrc[c].y)) fMaxHeight = (pfrc[c].y2 - pfrc[c].y); } //Inc the position of the next character along the x-axis. x += size.cx+1; } //Assign the coordinates to the font map. pfm->pfrcCoords = pfrc; pfm->fMaxHeight = fMaxHeight; pfm->fMaxWidth = fMaxWidth; //////////////////////////////////////////////// // Media translation to single channel format // //////////////////////////////////////////////// //Create the CiMedia object. pciMedia->Create(MT_IMBITMAP, IMFORMAT_BD8SC, dwImgHeight, dwImgWidth, NULL, NULL); //Get a pointer to the bits of the media object. pImgBits = (Byte*)pciMedia->RetrieveImageBitsRGB(); //Calculated the scanline of the current windows bitmap. //32-bit bitmap means there are 4 bytes per pixel RGB. nWidthBytes = (dwImgWidth+dwImgWidth+dwImgWidth) + CalculatePaddedBytes(dwImgWidth, 3); //Take a a single byte from the Windows bitmap //to create the single channel for(DWORD dwY=0;dwY> 4) -Results in-> A //Note: That during the creation of the font is done in gray color // schemes, as anything else will be filtered at this point. *pImgBits++ = (BYTE)((pBitmapBits[(nWidthBytes*dwY) + (dwX+dwX+dwX)] & 0xff));// >> 4); } } //Assign pci media object to the font map. pfm->pciMedia = (CiMedia*)pciMedia; //////////////////////////////////////////////// // CLEAN UP // //////////////////////////////////////////////// //Release all resources no longer needed. DeleteObject( hbmBitmap ); DeleteObject( hFont ); DeleteDC( hDC ); return EC_OK; //Return an okay status. } ///////////////////////////////////////////////////////////////////////////// //OS Dependant functions that can not be called due to their private status. //NOTE: Those functions that are listed below that are not private should // NOT be called unless you're using it in an entry point. ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class/Dependant: IX_Window // Name: Register // Overridden: No -- Independant Function. // Type: OS Dependant // Permission: Private // Desc: Register our window's class before we create our window. // Parameters: // 1) WNDPROC WndProc == Our WndProc function being passed for registration. // Return value: NONE ////////////////////////////////////////////////////////////////////////////////////////////////////// VOID IX_Window::RegClass(WNDPROC lpfnWndProc) { WNDCLASSEX wce; wce.cbSize = sizeof(WNDCLASSEX); wce.style = CS_HREDRAW|CS_VREDRAW; wce.lpfnWndProc = lpfnWndProc; wce.cbClsExtra = 0; wce.cbWndExtra = 0; wce.hInstance = m_hinst; wce.hIcon = m_hicon; wce.hCursor = LoadCursor(NULL, IDC_ARROW); wce.hbrBackground = (HBRUSH) COLOR_WINDOW; wce.lpszMenuName = m_lpMenuName ; wce.lpszClassName = m_lpClassName; wce.hIconSm = NULL; RegisterClassEx(&wce); } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SystemManager // Overridden: Yes // Date: 3/2/3--10:17PM // Type: Message Handling // Permission: Public // Desc: Acts as a slice in a message pump where the looping is controlled externally. // This function manages windows messages and only returns a false status if a quit // message is posted. // Note: Idling eats up 100% of CPU usage as the loop is constantly being accessed. // Nonidling releases the CPU and is only active on windows messages. // Parameters: // 1) bool& rbIdle == A reference to a boolean that notifies the system of an idling event. // Return value: // Bool == Returns true if the system manager is still operating, else returns false. ////////////////////////////////////////////////////////////////////////////////////////////////////// bool IX_Window::SystemManager(bool& rbIdle) { //Local variables. MSG msg; BOOL IsMsg = FALSE; //Zero memory our message for use. ZeroMemory(&msg, sizeof(msg)); //Fetch early message for quit check. PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE); //Precheck for quit message if(msg.message == WM_QUIT) return false; //If the application is active check for a message first. if(m_bActive) { if(m_bIdle) //Idling allows the program to take a majority CPU resources. IsMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE); else //Messaging only takes CPU resources when a message is dispatched. IsMsg = GetMessage(&msg, NULL, 0U, 0U); } else //Application is not active, so hold idling and allow only windows messages. IsMsg = GetMessage(&msg, NULL, 0U, 0U); //Translate and dispatch message which triggers WNDPROC if(IsMsg) { TranslateMessage(&msg); DispatchMessage(&msg); rbIdle = false; //System is not idling. } else //No windows message, then idling. if(m_bActive) rbIdle = true; //System is idling. //Return false if the quit message is issued. if(msg.message == WM_QUIT) return false; return true; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: FileExists // Overridden: Yes -- Independant Function. // Date: 7/4/05 // Type: OS Dependant // Permission: Public // Desc: Checks for the existence of the supplied file. // Note: Processing time will be shorter if a path is specified. // Parameters: // 1) cstr psFileName == A common string containing the name of the file. // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::FileExists(cstr psFileName) { //Locals WIN32_FIND_DATA wfd; //Find the file in the location specified. //Processing time will be shorter if a path is specified. //Invalid handle means the file was not found. if(FindFirstFile((LPCTSTR)psFileName, &wfd) == INVALID_HANDLE_VALUE) return OSE_FILE_NOTFOUND; else //File found. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: CreateFile // Overridden: Yes -- Independant Function. // Date: 12/7/04 // Type: OS Dependant // Permission: Public // Desc: Creates a file, or opens an already existing file depending on the parameters. // Parameters: // 1) void*** tvFileHandle == Returns a triset to a void object that contains file information. // This triset is required for use in reading & writing access. // 2) cstr psFileName == A pointer to a string containing the name of the file. // 3) Dword dwAccess == The method in which the file is accessed. // OS_FO_QUERY == Query device attributes. // OS_FO_READ == Read access // OS_FO_WRITE == Write access // 4) Dword dwShare == Designates whether the file is shared or not. // OS_FO_NO_SHARE == File can not be shared. // OS_FO_SHARE_DELETE == File can be deleted by other. // OS_FO_SHARE_READ == File can be read by others. // OS_FO_SHARE_WRITE == File can be written to by others. // 5) Dword dwCreationMethod == The method the file is created if it already exists. // OS_FO_CREATE_NEW == Creates a new file, and fails if it already exists. // OS_FO_CREATE_ALWAYS == Creates a new file and overwrites the original. // OS_FO_OPEN_EXISTING == Opens a file and fails if it doesn't exist. // OS_FO_OPEN_ALWAYS == Opens a file and creates it if it doesn't exist. // 6) Dword dwFlags == A flag or set of flags that define how the file behaves. // // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::CreateFile(void*** tvFileHandle, cstr psFileName, Dword dwAccess, Dword dwShare, Dword dwCreationMethod, Dword dwFlags) { //Locals Dword dwTranslate = (Dword)0; Dword dwCounter = 0x00000001; HANDLE hnd = NULL; //Check for null conditions when using pointers. if(tvFileHandle != NULL) if(*tvFileHandle == NULL) return ECERR_NULL; //Route the access properties correctly. switch(dwAccess) { case OS_FO_READ: dwAccess = GENERIC_READ; break; case OS_FO_WRITE: dwAccess = GENERIC_WRITE; break; case (OS_FO_READ | OS_FO_WRITE): dwAccess = GENERIC_READ | GENERIC_WRITE; break; } //Translate the sharing properties to windows. switch(dwShare) { case OS_FO_SHARE_DELETE: dwAccess = FILE_SHARE_DELETE; break; case OS_FO_SHARE_READ: dwAccess = FILE_SHARE_READ; break; case OS_FO_SHARE_WRITE: dwAccess = FILE_SHARE_WRITE; break; } //Translate Creation methods to windows. switch(dwCreationMethod) { case OS_FO_CREATE_NEW: dwCreationMethod = CREATE_NEW; break; case OS_FO_CREATE_ALWAYS: dwCreationMethod = CREATE_ALWAYS; break; case OS_FO_OPEN_EXISTING: dwCreationMethod = OPEN_EXISTING; break; case OS_FO_OPEN_ALWAYS: dwCreationMethod = OPEN_ALWAYS; break; } //Check for multiple flags and translate to windows. while(dwFlags > (Dword)0) { switch(dwFlags & dwCounter) { case OS_FO_ATTRIBUTE_ARCHIVE: dwTranslate |= FILE_ATTRIBUTE_ARCHIVE; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_HIDDEN: dwTranslate |= FILE_ATTRIBUTE_HIDDEN; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_NORMAL: dwTranslate |= FILE_ATTRIBUTE_NORMAL; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_OFFLINE: dwTranslate |= FILE_ATTRIBUTE_OFFLINE; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_READONLY: dwTranslate |= FILE_ATTRIBUTE_READONLY; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_SYSTEM: dwTranslate |= FILE_ATTRIBUTE_SYSTEM; dwFlags ^= dwCounter; break; case OS_FO_ATTRIBUTE_TEMPORARY: dwTranslate |= FILE_ATTRIBUTE_TEMPORARY; dwFlags ^= dwCounter; break; } dwCounter = dwCounter << 1; } //Create or open the file in the windows operating system using the parameters //specified by the user. //Both of the following parameters are left for future upgrades. //The 4th parameter is for windows security parameters. //The 7th parameter is use for a windows file template. hnd = ::CreateFile(psFileName, dwAccess, dwShare, NULL, dwCreationMethod, dwFlags, NULL); //An error occured. if(hnd == INVALID_HANDLE_VALUE) { *tvFileHandle = NULL; return TranslateError(GetLastError()); } //Set the file handle to the void handle. *tvFileHandle = (void**)hnd; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: ReadFile // Overridden: Yes -- Independant Function. // Date: 12/10/04 // Type: OS Dependant // Permission: Public // Desc: Reads a file for the current operating system, loading the data into a buffer. // Parameters: // 1) void** hvFile == A handle to a file that has already been opened or created. // 2) void* pvBuffer == A pointer to a buffer that contains the data that was loaded from the file. // 3) Dword dwBytesToRead == Specifies the amount of bytes to read from the file. // 4) Dword* pvBytesRead == A pointer to the amount of bytes read. // 5) Dword dwOverlapped == A value that specifies where in the file to read too. // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::ReadFile(void** hvFile, void* pvBuffer, Dword dwBytesToRead, Dword* pvBytesRead, Dword dwOverlapped) { //Locals //OVERLAPPED ol; //ol.Offset = dwOverlapped; //ol.hEvent = NULL; int nResults; //Read the file into memory. nResults = ::ReadFile(hvFile, pvBuffer, dwBytesToRead, pvBytesRead, NULL); //Check for end of file states. if(pvBytesRead != NULL) { if((nResults == 1) && (*pvBytesRead == 0)) return OSE_FILE_EOF; } else return ECERR_NULL; //Check for errors. if(nResults == 0) return TranslateError(GetLastError()); //Translate the error into Common OS errors. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: WriteFile // Overridden: Yes -- Independant Function. // Date: 12/11/04 // Type: OS Dependant // Permission: Public // Desc: Writes a file for the current operating system. // Parameters: // 1) void** hvFile == A handle to a file that has already been opened or created. // 2) void* pvBuffer == A pointer to a buffer that contains the data that will be written to the file. // 3) Dword dwBytesToWrite == Specifies the amount of bytes to write from the file. // 4) Dword* pvBytesWritten == A pointer to the amount of bytes written. // 5) Dword dwOverlapped == A value that specifies where in the file to read too. // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::WriteFile(void** hvFile, void* pvBuffer, Dword dwBytesToWrite, Dword* pvBytesWritten, Dword dwOverlapped) { //Locals int nResults; nResults = ::WriteFile(hvFile, pvBuffer, dwBytesToWrite, pvBytesWritten, NULL); //Check for errors. if(nResults == 0) return TranslateError(GetLastError()); //Translate the error into Common OS errors. return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: CloseFile // Overridden: Yes -- Independant Function. // Date: 12/12/04 // Type: OS Dependant // Permission: Public // Desc: Closes the file specified. // Parameters: // 1) void** hvFile == A handle to a file that has already been opened or created. // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::CloseFile(void** hvFile) { if(!CloseHandle(hvFile)) return TranslateError(GetLastError()); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetFileSize // Overridden: Yes -- Independant Function. // Date: 12/12/04 // Type: OS Dependant // Permission: Public // Desc: Fetchs the size of the file. // Parameters: // 1) void** hvFile == A handle to a file that has already been opened or created. // Return Value: // Qword == Returns the size of the file. ////////////////////////////////////////////////////////////////////////////////////////////////////// Qword IX_Window::GetFileSize(void** hvFile) { Qword qw; qw.LoDword = ::GetFileSize(hvFile, &qw.HiDword); return qw; } //--- kda (3/18/12) --- Provide the full path of the current directory. /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: GetCurrentDirectory // Overridden: Yes -- Independant Function. // Date: 3/18/12 // Type: OS Dependant // Permission: Public // Desc: Retrieve the current path. // Parameters: // Return Value: // string == The full path. ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::GetCurrentDirectory(string* szFullPath) { //Calculate the need size of the buffer. DWORD dwBufferSize = ::GetCurrentDirectory(0, NULL); char* buffer = NULL; try { buffer = new char[dwBufferSize]; //DWORD dwStatus = GetModuleFileName(hModule, buffer, 4096); DWORD dwStatus = ::GetCurrentDirectory(dwBufferSize, buffer); if(dwStatus > 0) { *szFullPath = buffer; // --- kda (3-18-12) --- If only I had known more about exceptions I wouldn't have used this convoluted system. return EC_OK; } } catch(...){} delete buffer; return ECERR_OUTOFMEM; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: DefaultOSProc // Overridden: No -- Independant Function. // Date: 2/28/06 // Type: OS Dependant // Permission: Public // Desc: Procedure for handling default operating system messages. // Parameters: // 1) Word wMsg == A message generated by the operating system. // 2) Dword dwwParam == A dword parameter for the message. // 3) Dword dwlParam == A dword parameter for the message. // Return value: // Dword == A 32-bit Dword value provided by the default processor that the OS // will understand. ////////////////////////////////////////////////////////////////////////////////////////////////////// Dword IX_Window::DefaultOSProc(Word wMsg, Dword dwwParam, Dword dwlParam) { if(m_hwnd != NULL) return DefWindowProc(m_hwnd, (UINT)wMsg, (WPARAM)dwwParam, (LPARAM)dwlParam); else return 0; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: TranslateError // Overridden: No -- Independant Function. // Date: 4/13/3 // Type: OS Dependant. // Permission: Private // Desc: Translates the windows error into an OS error for application to manage. // Return Value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::TranslateError(DWORD dwErr) { switch(dwErr) { case ERROR_OUTOFMEMORY: return ECERR_OUTOFMEM; case ERROR_NOT_ENOUGH_MEMORY: return OSE_NOT_ENOUGH_MEM; case ERROR_INVALID_HANDLE: return OSE_INVALID_HANDLE; case ERROR_INVALID_DLL: return OSE_INVALID_DYNAMIC_LIBRARY; case ERROR_INVALID_WINDOW_HANDLE: return OSE_INVALID_WIN_HANDLE; case ERROR_INVALID_MENU_HANDLE: return OSE_INVALID_MENU_HANDLE; case ERROR_INVALID_CURSOR_HANDLE: return OSE_INVALID_CUR_HANDLE; case ERROR_INVALID_NAME: return OSE_INVALID_NAME; case ERROR_SHUTDOWN_IN_PROGRESS: return OSE_SHUTDOWN; case ERROR_FILE_CORRUPT: return OSE_FILE_CORRUPT; case ERROR_ALREADY_EXISTS: return OSE_FILE_EXISTS; case ERROR_HANDLE_EOF: return OSE_FILE_EOF; default: return ECERR_UNKNOWN; } } ///////////////////////////////////////////////////////////////////////////// //OS Dependant registration & creation functions. ///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowState // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Sets the state of the window. // Parameters: // 1) int CmdShow == The state of the window. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowState(int CmdShow) { m_nCmdShow = CmdShow; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowHMenu // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Sets the window's menu by providing a handle to the object. // Parameters: // 1) HMENU hmenu == A handle to the menu object. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowHMenu(HMENU hmenu) { m_hmenu = hmenu; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetHInstance // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Sets the instance of the program most likely supplied by the operating system. // Parameters: // 1) HINSTANCE hinst == A handle to an instance of the program. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetHInstance(HINSTANCE hinst) { m_hinst = hinst; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowHIcon // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Sets the instance of the program most likely supplied by the operating system. // Parameters: // 1) HICON hicon == A handle to an icon object to use for the window. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowHIcon(HICON hicon) { m_hicon = hicon; return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowIcon // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Set the Icon for the Window to use. // NOTE: USE BEFORE REGISTRING A WINDOWS CLASS or before using the Create Member. // Parameters: // 1) Word wIconID == A Resource ID to an icon. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowIcon(Word wIconID) { m_hicon = LoadIcon(m_hinst, MAKEINTRESOURCE(wIconID)); return EC_OK; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // Class: IX_Window // Name: SetWindowMenu // Overridden: No -- Independant Function. // Date: 3/3/3 // Type: OS Dependant // Permission: Public // Desc: Set the Menu for the Window to use. // NOTE: USE BEFORE REGISTRING A WINDOWS CLASS or before using the Create Member. // Parameters: // 1) Word wMenuID == A Resource ID to a menu.. // Return value: // OS_ERR == Returns one of the defined error states listed in OS_ErrorCodes.h ////////////////////////////////////////////////////////////////////////////////////////////////////// OS_ERR IX_Window::SetWindowMenu(Word wMenuID) { m_hmenu = LoadMenu(m_hinst, MAKEINTRESOURCE(wMenuID)); m_lpMenuName = MAKEINTRESOURCE(wMenuID); return EC_OK; }